using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Data;
using System.Windows.Interactivity;
namespace Microsoft.Expression.Interactivity
{
	internal static class DataBindingHelper
	{
		private static Dictionary<Type, IList<DependencyProperty>> DependenciesPropertyCache = new Dictionary<Type, IList<DependencyProperty>>();
		public static void EnsureDataBindingUpToDateOnMembers(DependencyObject dpObject)
		{
			IList<DependencyProperty> list = null;
			if (!DataBindingHelper.DependenciesPropertyCache.TryGetValue(dpObject.GetType(), ref list))
			{
				list = new List<DependencyProperty>();
				for (Type type = dpObject.GetType(); type != null; type = type.get_BaseType())
				{
					FieldInfo[] fields = type.GetFields();
					FieldInfo[] array = fields;
					for (int i = 0; i < array.Length; i++)
					{
						FieldInfo fieldInfo = array[i];
						if (fieldInfo.get_IsPublic() && fieldInfo.get_FieldType() == typeof(DependencyProperty))
						{
							DependencyProperty dependencyProperty = fieldInfo.GetValue(null) as DependencyProperty;
							if (dependencyProperty != null)
							{
								list.Add(dependencyProperty);
							}
						}
					}
				}
				DataBindingHelper.DependenciesPropertyCache.set_Item(dpObject.GetType(), list);
			}
			if (list == null)
			{
				return;
			}
			using (IEnumerator<DependencyProperty> enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					DependencyProperty current = enumerator.get_Current();
					DataBindingHelper.EnsureBindingUpToDate(dpObject, current);
				}
			}
		}
		public static void EnsureDataBindingOnActionsUpToDate(TriggerBase<DependencyObject> trigger)
		{
			using (IEnumerator<TriggerAction> enumerator = trigger.Actions.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					TriggerAction current = enumerator.get_Current();
					DataBindingHelper.EnsureDataBindingUpToDateOnMembers(current);
				}
			}
		}
		public static void EnsureBindingUpToDate(DependencyObject target, DependencyProperty dp)
		{
			BindingExpression bindingExpression = target.ReadLocalValue(dp) as BindingExpression;
			if (bindingExpression != null)
			{
				target.ClearValue(dp);
				target.SetValue(dp, bindingExpression);
			}
		}
	}
}
